import json
import logging
import os
from flask import Flask, request
from flask_cors import CORS

from bean.Response import Meta
from bean.Response import Response
from controller.RecommendController import  recommend_api
from controller.MetastoreController import  metastore_api
from service.MovieDaoService import get_movie_by_id, get_new_movie_list, get_recommend_movie_list


app = Flask(__name__)
CORS(app, resources=r'/*')


app.register_blueprint(recommend_api)
app.register_blueprint(metastore_api)

@app.route('/')
def hello_world():
    return "ok"


@app.route('/movie/detail/<int:movie_id>')
def detail(movie_id):
    movie_info_list = get_movie_by_id(movie_id)
    movie_info = movie_info_list[0]
    movie_id = movie_info[0]
    movie_name = movie_info[1]
    poster_url = movie_info[2]
    movie_desc = movie_info[3]
    download_info = json.loads(movie_info[4])
    data_date = movie_info[5]
    recommend = movie_info[6]

    result = {
        "id": movie_id,
        "name": movie_name,
        "poster_url": poster_url,
        "movie_desc": movie_desc,
        "download_info": download_info,
        "data_date": data_date,
        "recommend": recommend
    }

    meta = Meta("OK", 200)
    response = Response(meta.__dict__, result)
    return json.dumps(response.__dict__)


@app.route('/movie/new_movie_list')
def new_movie_list():
    new_movie = []

    movie_list = get_new_movie_list()
    for movie_info in movie_list:
        movie_id = movie_info[0]
        movie_name = movie_info[1]
        data_date = movie_info[2]
        recommend = movie_info[3]

        result = {
            "id": movie_id,
            "name": movie_name,
            "data_date": data_date,
            "recommend": recommend
        }
        new_movie.append(result)

    meta = Meta("OK", 200)
    response = Response(meta.__dict__, new_movie)
    return json.dumps(response.__dict__)


@app.route('/movie/recommend_movie_list')
def recommend_movie_list():
    recommend_movie = []
    movie_list = get_recommend_movie_list()
    for movie_info in movie_list:
        movie_id = movie_info[0]
        movie_name = movie_info[1]
        poster_url = movie_info[2]

        result = {
            "id": movie_id,
            "name": movie_name,
            "poster_url": poster_url
        }
        recommend_movie.append(result)

    meta = Meta("OK", 200)
    response = Response(meta.__dict__, recommend_movie)
    return json.dumps(response.__dict__)


def allow_file(filename):
    allow_list = ['csv', 'tsv']
    a = filename.split('.')[1]
    if a in allow_list:
        return True
    else:
        return False


@app.route('/api/private/v1/upload', methods=['POST', 'GET'])
def upload():
    file = request.files['file']
    if not file:
        return 'upload fail, upload is not file.'
    if allow_file(file.filename):
        file.save(os.path.join('./upload/', file.filename))
        logging.info('上传文件成功')
        # upload_file_to_hdfs(file.filename)

        return 'OK'
    else:
        return 'NO'


@app.route('/api/private/v1/login', methods=['POST', 'GET'])
def login():
    result = {
        "data": {
            "id": 500, "rid": 0,
            "username": "admin",
            "mobile": "12345678", "email": "adsfad@qq.com",
            "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6Ik"
        },
        "meta": {"msg": "登录成功", "status": 200}
    }
    return json.dumps(result)


@app.route('/api/private/v1/menus', methods=['POST', 'GET'])
def menus():
    result = {
        "data": [
            {"id": 125, "authName": "用户管理", "path": "users",
             "children": [{"id": 110, "authName": "用户列表", "path": "users", "children": [], "order": None}],
             "order": 1},
            {"id": 103, "authName": "权限管理", "path": "rights",
             "children": [{"id": 111, "authName": "角色列表", "path": "roles", "children": [], "order": None},
                          {"id": 112, "authName": "权限列表", "path": "rights", "children": [], "order": None}],
             "order": 2},
            # { "id": 101, "authName": "商品管理", "path": "goods", "children": [ { "id": 104, "authName": "商品列表", "path": "goods", "children": [], "order": 1 }, { "id": 115, "authName": "分类参数", "path": "params", "children": [], "order": 2 }, { "id": 121, "authName": "商品分类", "path": "categories", "children": [], "order": 3 } ], "order": 3 },
            # { "id": 102, "authName": "订单管理", "path": "orders", "children": [ { "id": 107, "authName": "订单列表", "path": "orders", "children": [], "order": None } ], "order": 4 },
            {"id": 145, "authName": "数据管理", "path": "data_manager",
             "children": [{"id": 146, "authName": "数据导入", "path": "syn_data", "children": [], "order": 1}],
             "order": 3} ,
            {"id": 102, "authName": "元数据中心", "path": "data_manager",
             "children": [{"id": 148, "authName": "字段列表", "path": "metastore", "children": [], "order": 1}],
             "order": 3},
            {"id": 101, "authName": "小工具", "path": "small_tool",
             "children": [{"id": 148, "authName": "字段列表", "path": "small_tool/cost", "children": [], "order": 1}],
             "order": 4}
        ],
        "meta": {"msg": "获取菜单列表成功", "status": 200}
    }
    return json.dumps(result)





if __name__ == "__main__":
    # app.run(debug=True, port=8888)
    app.run(host='0.0.0.0', debug=True, port=8888)
